<?php
require_once str_replace("//","/",$_SESSION["app_root"]."config/DB.conf.php");
class Database
{
	public $connectionHandler;
	public function Database($db_database=DB_DATABASE, $db_host=DB_HOST, $db_user=DB_USER, $db_pass=DB_PASS)
	{
		if($this->connectionHandler=mysql_connect($db_host,$db_user,$db_pass) or die("Connection error connector 8".mysql_error()))
		{
			mysql_select_db($db_database);
			return true;
		}
		else
		{
			return false;
		}
	}

	public function close()
	{
		
		if(mysql_close($this->connectionHandler))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public function exists($query)
	{
		$result=mysql_query($query) or die(mysql_error());
		return mysql_num_rows($result)>=1?true:false;
		//var_dump($result);
	}

	public function getRow($table="", $selArray="*", $whereArray=1)
	{
		$query="SELECT ";
		if($selArray!="*")
		{
			foreach($selArray as $val)
			{
				$query.=$val." , ";
			}
			$query=substr($query,0,strlen($query)-2);
		}
		else
		{
			$query.="  * ";
		}
		$query.=" FROM ".$table." WHERE ";
		if($selArray!=1)
		{
			$i=1;
			foreach($whereArray as $name=>$val)
			{
				$query.=$name."='".$val."' ";
				if($i<sizeof($whereArray))
					$query.=" AND ";
				if($i==sizeof($whereArray))
					$query.=" LIMIT 1 ";
				
				$i++;
			}
		}
		else
		{
			$query.=" 1 ";
		}
// 		echo "<br />".$query;
		$result=mysql_query($query) or die(mysql_error());
		$toReturn=false;
		if($row=mysql_fetch_assoc($result))
			foreach($row as $name=>$val)
			{
				$toReturn[$name]=$val;
			}
		return $toReturn;
	}

	public function getRows($table="", $selArray="*", $whereArray=1, $orderby=1)
	{
		
		$query="SELECT ";
		if($selArray!="*")
		{
			foreach($selArray as $val)
			{
				$query.=$val." , ";
			}
		}
		else
		{
			
			$query.=" *  ";
		}
		$query=substr($query,0,strlen($query)-2);
		$query.=" FROM ".$table.=" WHERE ";
		if($whereArray!=1)
		{
			$i=1;
			foreach($whereArray as $name=>$val)
			{
				$query.="`".$name."`='".$val."' ";
				if($i<sizeof($whereArray))
					$query.=" AND ";
				if($i==sizeof($whereArray))
					$query.="";
				$i++;
			}
		}
		else
		{
			$query.=" 1 ";
		}

		if($orderby!=1)
		{
			$query.=" ORDER BY ".$orderby;
		}
// 		echo "<br />".$query;
		$result=mysql_query($query) or die(mysql_error());
		$toReturn=false;
		if($result)
		{
			$toReturn=array();
			while($row=mysql_fetch_assoc($result))
				array_push($toReturn,$row);
		}
		return $toReturn;
	}

	public function insertRow($table="", $dataArray="*", $whereArray=1)
	{
		//echo($dataArray);
		$tblFields = "";
		$tblVals = "";
		foreach($dataArray as $name=>$val)
		{
			$tblFields.=", `".$name."` ";
			$tblVals.=", '".$val."' ";
		}
		$tblFields=substr($tblFields,1);
		$tblVals=substr($tblVals,1);
		$builtQuery="
			INSERT INTO ".str_replace(' ','',$table)."
			(
				".$tblFields."
			)
			VALUES
			(
				".$tblVals."
			)";

		$result=mysql_query($builtQuery) or die(mysql_error());
		if($result)
			return mysql_insert_id();
		else
			return false;
	}
	
	
	public function updateRow($table="", $dataArray="*", $whereArray=1)
	{
		//vp($whereArray);
		foreach($dataArray as $name=>$val)
		{
			$tblSet.=", `".$name."` ="." '".$val."' ";
			//$tblFields.=", `".$name."` ";
			//$tblVals.=", '".$val."' ";
		}
		//$tblFields=substr($tblFields,1);
		//$tblVals=substr($tblVals,1);
		$tblSet=substr($tblSet,1);
		$builtQuery="
			UPDATE ".str_replace(' ','',$table)." SET
			
				".$tblSet."
			WHERE 
			";
		if($whereArray!=1)
		{
			$i=1;
			foreach($whereArray as $name=>$val)
			{
				$builtQuery.="`".$name."`='".$val."' ";
				if($i<sizeof($whereArray))
					$builtQuery.=" AND ";
				if($i==sizeof($whereArray))
					$builtQuery.="";
				$i++;
			}
		}
		else
		{
			$builtQuery.=" ThrowAnError ";
		}
		$builtQuery;
		$result=mysql_query($builtQuery) or die(mysql_error());
		if($result)
			return true;
		else
			return false;
	}
	
	function rawDelete($qString)
	{
		//echo $qString;
		$result = mysql_query($qString) or die(mysql_error());
		return mysql_insert_id();
	}
	
	function rawUpdate($qString)
	{
		$result = mysql_query($qString) or die(mysql_error());
		return $result;
	}

	function rawInsert($qString)
	{
		$result = mysql_query($qString) or die(mysql_error());
		return mysql_insert_id();
	}
	
	function rawQuery($qString)
	{
		$result = mysql_query($qString) or die(mysql_error());
		return mysql_insert_id();
	}

	function rawSelect($qString)
	{	
		//vp($qString);
		$result = mysql_query($qString) or die(mysql_error());
		if($result)
		{
			$toReturn=array();
			while($row=mysql_fetch_assoc($result))
				array_push($toReturn,$row);
			return $toReturn;
		}
		else
			return false;
		
	}
}

?>